{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:05:10.520703Z",
     "start_time": "2025-01-06T16:04:54.731541Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import random\n",
    "import time\n",
    "import numpy as np\n",
    "a = []\n",
    "for i in range(100000000):\n",
    "    a.append(random.random())\n",
    "print('随机完成')\n",
    "\n",
    "#ndarray优于python的list\n",
    "t1 = time.time()\n",
    "sum1 = sum(a)\n",
    "t2 = time.time()\n",
    "\n",
    "b = np.array(a)\n",
    "t4 = time.time()\n",
    "sum3 = np.sum(b)\n",
    "t5 = time.time()\n",
    "print(t2 - t1, t5 - t4)"
   ],
   "id": "63b6fa430b82c4dc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机完成\n",
      "0.8754863739013672 0.11199951171875\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:05:59.178627Z",
     "start_time": "2025-01-06T16:05:59.174081Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.array([1,2,3])\n",
    "print(t1)\n",
    "print(type(t1))"
   ],
   "id": "c909d6ee88b2d020",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:08:52.947191Z",
     "start_time": "2025-01-06T16:08:52.943174Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(type(range(10)))\n",
    "t2  = np.array(range(10))\n",
    "print(t2)\n",
    "print(type(t2))\n"
   ],
   "id": "a1032413d8692919",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'range'>\n",
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:10:08.820301Z",
     "start_time": "2025-01-06T16:10:08.816044Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t3 = np.arange(0,10,2)\n",
    "print(t3)\n",
    "print(type(t3))\n"
   ],
   "id": "da97a57b44c06e09",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 2 4 6 8]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:10:59.569591Z",
     "start_time": "2025-01-06T16:10:59.565742Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#二维列表转ndarray\n",
    "import numpy as np\n",
    "list2 = [[1,2],[3,4],[5,6]]\n",
    "twoArray = np.array(list2)\n",
    "print(type(twoArray))\n",
    "print(twoArray)\n",
    "print(list2)"
   ],
   "id": "972f543acf4bf032",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[[1, 2], [3, 4], [5, 6]]\n"
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:12:12.494122Z",
     "start_time": "2025-01-06T16:12:12.485771Z"
    }
   },
   "cell_type": "code",
   "source": "twoArray.tolist() #ndarray转二维列表",
   "id": "bb19e86a3dcc3bc0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 2], [3, 4], [5, 6]]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:13:59.784592Z",
     "start_time": "2025-01-06T16:13:59.770029Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list2 = [[1,2],[3,4],[5,6]]\n",
    "twoArray = np.array(list2)#二维列表转ndarray\n",
    "print(twoArray.ndim)#维度\n",
    "print(twoArray.shape)#维度\n",
    "print(twoArray.size)#元素个数\n",
    "print(twoArray.dtype)#数据类型"
   ],
   "id": "2dfdf234984ba9e4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(3, 2)\n",
      "6\n",
      "int64\n"
     ]
    }
   ],
   "execution_count": 40
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:15:14.739046Z",
     "start_time": "2025-01-06T16:15:14.735237Z"
    }
   },
   "cell_type": "code",
   "source": [
    "four =  np.array([[1,2,3],[4,5,6]])\n",
    "print(four)\n",
    "four1 = four\n",
    "print(id(four))"
   ],
   "id": "867b69f72da06ff3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "1917063357008\n"
     ]
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:15:41.619357Z",
     "start_time": "2025-01-06T16:15:41.615582Z"
    }
   },
   "cell_type": "code",
   "source": [
    "four.shape = (3,2)\n",
    "print(id(four))\n",
    "print(id(four1))\n",
    "print(four)"
   ],
   "id": "3f38b46b05913cad",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1917063357008\n",
      "1917063357008\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:16:52.201731Z",
     "start_time": "2025-01-06T16:16:52.198589Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('-'*50)\n",
    "four2 = four.reshape(3,2)\n",
    "print(four)\n",
    "print(id(four))\n",
    "print(id(four2))\n"
   ],
   "id": "1e4cdb03d3527860",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "1917063357008\n",
      "1917066663792\n"
     ]
    }
   ],
   "execution_count": 44
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:21:33.338672Z",
     "start_time": "2025-01-06T16:21:33.334978Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#多位变成一维\n",
    "five = four.reshape((6,),order = 'C')\n",
    "#默认“C”以行优先，“F”以列优先\n",
    "six = four.flatten() #一维化\n",
    "print(five)\n",
    "print('-'*50)\n",
    "print(six)"
   ],
   "id": "565a97f3ca5fc2e9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 3 5 2 4 6]\n",
      "--------------------------------------------------\n",
      "[1 2 3 4 5 6]\n"
     ]
    }
   ],
   "execution_count": 46
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:21:54.642457Z",
     "start_time": "2025-01-06T16:21:54.638293Z"
    }
   },
   "cell_type": "code",
   "source": [
    "seven = five.reshape(3,2)#改变形状\n",
    "seven"
   ],
   "id": "8ace72c7648e3285",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 3],\n",
       "       [5, 2],\n",
       "       [4, 6]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 47
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:23:48.169969Z",
     "start_time": "2025-01-06T16:23:48.165765Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('-'*50)\n",
    "t = np.arange(24)\n",
    "print(t)\n",
    "print(f'shape{t.shape}')\n",
    "print(t.ndim)\n",
    "#转换成二维\n",
    "t1 = t.reshape((4,6))\n",
    "print(t1)\n",
    "print(t1.shape)"
   ],
   "id": "7ad914755da3190e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n",
      "shape(24,)\n",
      "1\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "(4, 6)\n"
     ]
    }
   ],
   "execution_count": 49
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:25:33.408535Z",
     "start_time": "2025-01-06T16:25:33.404674Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t2 = t.reshape((2,3,4))\n",
    "print(t2)\n",
    "print(t2.shape)\n",
    "print(t2.ndim)"
   ],
   "id": "f15b4ac3e2654f20",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "(2, 3, 4)\n",
      "3\n"
     ]
    }
   ],
   "execution_count": 50
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:27:23.990742Z",
     "start_time": "2025-01-06T16:27:22.835187Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#将数组转化为列表tolist()\n",
    "a = np.array([9,12,88,14,25])\n",
    "print(type(a))\n",
    "list_a = a.tolist()\n",
    "print(list_a)\n",
    "print(type(list_a))"
   ],
   "id": "24bfbbbbb15871ee",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[9, 12, 88, 14, 25]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "execution_count": 51
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:28:22.745252Z",
     "start_time": "2025-01-06T16:28:22.741855Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import random\n",
    "f = np.array([1,2,3,4,5], dtype = np.int16)\n",
    "print(f.itemsize) # 1 np.int8(一个字节)\n",
    "# 获取数据类型\n",
    "print(f.dtype)"
   ],
   "id": "fb40125f28f65110",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "int16\n"
     ]
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:28:24.201325Z",
     "start_time": "2025-01-06T16:28:24.197461Z"
    }
   },
   "cell_type": "code",
   "source": [
    "f1 = f.astype(np.int64)\n",
    "print(f1.dtype)\n",
    "# 拓展随机生成小数\n",
    "# 使用python语法，保留两位\n",
    "print(round(random.random(),2))\n",
    "arr = np.array([random.random() for i in range(10)])\n",
    "# 取小数点后两位\n",
    "print(np.round(arr,2))"
   ],
   "id": "95a5713458d5bc61",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int64\n",
      "0.37\n",
      "[0.14 0.54 0.11 0.65 0.25 0.57 0.59 0.22 0.1  0.11]\n"
     ]
    }
   ],
   "execution_count": 54
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:28:58.186117Z",
     "start_time": "2025-01-06T16:28:58.181874Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#数组的计算\n",
    "t1 =np.arange(24).reshape((6,4))\n",
    "print(t1+2)\n",
    "print(\"-\"*20)\n",
    "print(t1*2)\n",
    "print(\"-\"*20)\n",
    "print(t1/2)"
   ],
   "id": "ca411080c5d0e5ba",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]\n",
      " [22 23 24 25]]\n",
      "--------------------\n",
      "[[ 0  2  4  6]\n",
      " [ 8 10 12 14]\n",
      " [16 18 20 22]\n",
      " [24 26 28 30]\n",
      " [32 34 36 38]\n",
      " [40 42 44 46]]\n",
      "--------------------\n",
      "[[ 0.   0.5  1.   1.5]\n",
      " [ 2.   2.5  3.   3.5]\n",
      " [ 4.   4.5  5.   5.5]\n",
      " [ 6.   6.5  7.   7.5]\n",
      " [ 8.   8.5  9.   9.5]\n",
      " [10.  10.5 11.  11.5]]\n"
     ]
    }
   ],
   "execution_count": 56
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:29:43.232345Z",
     "start_time": "2025-01-06T16:29:43.228179Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#同种数组的操作\n",
    "t1 = np.arange(24).reshape((6,4))\n",
    "t2 = np.arange(100,124).reshape((6,4))\n",
    "print(t1+t2)\n",
    "print(t1*t2)"
   ],
   "id": "de90eebd0eaefac7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[100 102 104 106]\n",
      " [108 110 112 114]\n",
      " [116 118 120 122]\n",
      " [124 126 128 130]\n",
      " [132 134 136 138]\n",
      " [140 142 144 146]]\n",
      "[[   0  101  204  309]\n",
      " [ 416  525  636  749]\n",
      " [ 864  981 1100 1221]\n",
      " [1344 1469 1596 1725]\n",
      " [1856 1989 2124 2261]\n",
      " [2400 2541 2684 2829]]\n"
     ]
    }
   ],
   "execution_count": 58
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:29:42.164867Z",
     "start_time": "2025-01-06T16:29:42.151564Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#不同数组的操作\n",
    "t1 = np.arange(24).reshape((4,6))\n",
    "t2 = np.arange(18).reshape((3,6))\n",
    "print(t1)\n",
    "print(t2)\n",
    "print(t1-t2)"
   ],
   "id": "474431895917b3b8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (4,6) (3,6) ",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[57], line 6\u001B[0m\n\u001B[0;32m      4\u001B[0m \u001B[38;5;28mprint\u001B[39m(t1)\n\u001B[0;32m      5\u001B[0m \u001B[38;5;28mprint\u001B[39m(t2)\n\u001B[1;32m----> 6\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[43mt1\u001B[49m\u001B[38;5;241;43m-\u001B[39;49m\u001B[43mt2\u001B[49m)\n",
      "\u001B[1;31mValueError\u001B[0m: operands could not be broadcast together with shapes (4,6) (3,6) "
     ]
    }
   ],
   "execution_count": 57
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:30:14.642722Z",
     "start_time": "2025-01-06T16:30:14.639505Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#列相同\n",
    "t1 = np.arange(24).reshape((4,6))\n",
    "t2 = np.arange(0,6)\n",
    "print(t1-t2)"
   ],
   "id": "8d00fd4d843813a1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  0  0  0  0  0]\n",
      " [ 6  6  6  6  6  6]\n",
      " [12 12 12 12 12 12]\n",
      " [18 18 18 18 18 18]]\n"
     ]
    }
   ],
   "execution_count": 60
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:30:13.686084Z",
     "start_time": "2025-01-06T16:30:13.683009Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#行相同\n",
    "t1 = np.arange(24).reshape((4,6))\n",
    "t2 = np.arange(0,4).reshape((4,1))\n",
    "print(t1-t2)"
   ],
   "id": "c16489068661d0fa",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 5  6  7  8  9 10]\n",
      " [10 11 12 13 14 15]\n",
      " [15 16 17 18 19 20]]\n"
     ]
    }
   ],
   "execution_count": 59
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "#列不同\n",
    "t1 = np.arange(24).reshape((4,6))\n",
    "t2 = np.arange(0,6).reshape((1,6))"
   ],
   "id": "245f10de0d6797bb"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:30:36.063795Z",
     "start_time": "2025-01-06T16:30:36.007738Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#轴\n",
    "import numpy as np\n",
    "a = np.array([[1,2,3],[4,5,6]])\n",
    "print(np.sum(a,axis=0)) # [5 7 9]\n",
    "print(np.sum(a,axis = 1)) # [ 6 15]\n",
    "print(np.sum(a))\n",
    "a=np.arange(27).reshape((3,3,3))\n",
    "print(a)\n",
    "b=np.sum(a, axis=0)\n",
    "print(\"-\"*20)\n",
    "print(b)\n",
    "c=np.sum(a, axis=1)\n",
    "print(\"-\"*20)\n",
    "print(c)\n",
    "d=np.sum(a, axis=2)\n",
    "print(\"-\"*20)\n",
    "print(d)"
   ],
   "id": "aadbc1a59c95034b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 7 9]\n",
      "[ 6 15]\n",
      "21\n",
      "[[[ 0  1  2]\n",
      "  [ 3  4  5]\n",
      "  [ 6  7  8]]\n",
      "\n",
      " [[ 9 10 11]\n",
      "  [12 13 14]\n",
      "  [15 16 17]]\n",
      "\n",
      " [[18 19 20]\n",
      "  [21 22 23]\n",
      "  [24 25 26]]]\n",
      "--------------------\n",
      "[[27 30 33]\n",
      " [36 39 42]\n",
      " [45 48 51]]\n",
      "--------------------\n",
      "[[ 9 12 15]\n",
      " [36 39 42]\n",
      " [63 66 69]]\n",
      "--------------------\n",
      "[[ 3 12 21]\n",
      " [30 39 48]\n",
      " [57 66 75]]\n"
     ]
    }
   ],
   "execution_count": 61
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:32:11.007432Z",
     "start_time": "2025-01-06T16:32:10.999416Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#数组的索引与切片\n",
    "import numpy as np\n",
    "t1 = np.arange(24).reshape(4,6)\n",
    "print(t1)\n",
    "print('*'*20)\n",
    "print(t1[1]) # 取一行(一行代表是一条数据，索引也是从0开始的) print(t1[1,:]) # 取一行\n",
    "print('*'*20)\n",
    "print(t1[1:])# 取连续的多行\n",
    "print('*'*20)\n",
    "print(t1[1:3,:])# 取连续的多行\n",
    "print('*'*20)\n",
    "print(t1[[0,2,3]])# 取不连续的多行\n",
    "print('*'*20)\n",
    "print(t1[[0,2,3],:])# 取不连续的多行\n",
    "print('*'*20)\n",
    "print(t1[:,1])# 取一列\n",
    "print('*'*20)\n",
    "print(t1[:,1:])# 连续的多列\n",
    "print('*'*20)\n",
    "print(t1[:,[0,2,3]])# 取不连续的多列\n",
    "print('*'*20)\n",
    "print(t1[2,3])# # 取某一个值,三行四列\n",
    "print('*'*20)"
   ],
   "id": "7a13ab14b53fc373",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[ 6  7  8  9 10 11]\n",
      "********************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n",
      "********************\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[ 1  7 13 19]\n",
      "********************\n",
      "[[ 1  2  3  4  5]\n",
      " [ 7  8  9 10 11]\n",
      " [13 14 15 16 17]\n",
      " [19 20 21 22 23]]\n",
      "********************\n",
      "[[ 0  2  3]\n",
      " [ 6  8  9]\n",
      " [12 14 15]\n",
      " [18 20 21]]\n",
      "********************\n",
      "15\n",
      "********************\n"
     ]
    }
   ],
   "execution_count": 62
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:33:56.115102Z",
     "start_time": "2025-01-06T16:33:56.107885Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#数组的数值修改\n",
    "import numpy as np\n",
    "t = np.arange( 24 ).reshape( 4, 6 )\n",
    "# 修改某一行的值\n",
    "t[1, :] = 0\n",
    "# 修改某一列的值\n",
    "t[:, 1] = 0\n",
    "# 修改连续多行\n",
    "t[1:3, :] = 0\n",
    "# 修改连续多列\n",
    "t[:, 1:4] = 0\n",
    "t[t < 10] = 0\n",
    "# print(t)\n",
    "# 使用逻辑判断\n",
    "# np.logical_and & # np.logical_or |\n",
    "# np.logical_not ~\n",
    "t[(t > 2) & (t < 6)] = 0 # 与\n",
    "t[(t < 2) | (t > 6)] = 0 # 或\n",
    "t[~(t > 6)] = 0 # 非\n",
    "print(t)\n",
    "t=t.clip(10,18)\n",
    "print(t)\n",
    "# 拓 展\n",
    "#三目运算（ np.where(condition, x, y)满足条件(condition)，输出 x，不满足输出 y）\n",
    "score = np.array( [[80, 88], [82, 81], [75, 81]] )\n",
    "result = np.where( score > 80, True, False )\n",
    "print( result )\n"
   ],
   "id": "99ae7b7ac02e8fdc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0]]\n",
      "[[10 10 10 10 10 10]\n",
      " [10 10 10 10 10 10]\n",
      " [10 10 10 10 10 10]\n",
      " [10 10 10 10 10 10]]\n",
      "[[False  True]\n",
      " [ True  True]\n",
      " [False  True]]\n"
     ]
    }
   ],
   "execution_count": 64
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
